import os
import time

import pytest

from common.readData import readData
from common.Assert import Assert
from common.DButils import select,back_up,rollback
from common import loginOperater
from selenium import webdriver
from common.comDB import comDB
from common.serviceWeb import ServiceWeb
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

com_db=comDB()
read_data=readData()
class TestAccount():
    @pytest.mark.parametrize("username,password,oldpassword,newpassword,repassword,expect", read_data.read_data(6,6))
    def test_account(self,username,password,oldpassword,newpassword,repassword,expect ):
        service = ServiceWeb()

        backup = []

        try:
            # 拉起浏览器
            chromeDriver = service.get_driver("http://localhost:90/")
            # 链接数据库
            con, cursor = com_db.get_con()
            # 数据库备份
            backup = back_up("user")
            # 登录
            login = loginOperater.LoginOperater()
            login.login(chromeDriver, username, password)
            time.sleep(1)

            #进入账户安全
            chromeDriver.get("http://localhost:90/user/personal/toSecurity.html")
            chromeDriver.maximize_window()

            # 初始化显式等待对象
            wait = WebDriverWait(chromeDriver, 10)

            #修改信息
            el6=wait.until(
                EC.visibility_of_element_located((By.ID,"oldpwd"))
            )
            el6.send_keys(oldpassword)

            el7=wait.until(
                EC.visibility_of_element_located((By.ID,"newpwd"))
            )
            el7.send_keys(newpassword)

            el8 = wait.until(
                EC.visibility_of_element_located((By.ID, "confirmpwd"))
            )
            el8.send_keys(repassword)

            el9=chromeDriver.find_element(By.ID,"submit")
            el9.click()
            time.sleep(1)

            #查询用户信息
            sql1= "SELECT password FROM user WHERE  username=%s"
            """cursor.execute(sql1, (username,),)
            con.commit()"""
            result = select(sql1,(username,),)[0][0]
            print(result)

            # 断言
            screenshot_dir = "D:\pythonproject\自动化_1封装\失败截图\account"
            screenshot_path = f"{screenshot_dir}/{username}失败截图.png"
            Assert(chromeDriver, result, expect, screenshot_path)

        finally:
            # 数据库恢复
            rollback(backup, "user")
            # 资源清理
            cursor.close()
            con.close()
            chromeDriver.quit()
